package com.blyat.xsoft.kernel.component.security;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.web.access.intercept.DefaultFilterInvocationSecurityMetadataSource;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
import org.springframework.security.web.util.matcher.RequestMatcher;

import java.util.Collection;
import java.util.LinkedHashMap;

/**
 * Created by rxiu on 2018/3/20.
 */
@Deprecated
public class FilterInvocationSecurityMetadataSourceFactoryBean implements FactoryBean<FilterInvocationSecurityMetadataSource> {

/*    @Autowired
    AuthorityService authorityService;*/

    @Override
    public FilterInvocationSecurityMetadataSource getObject() throws Exception {
        return new DefaultFilterInvocationSecurityMetadataSource(getResources());
    }

    @Override
    public Class<FilterInvocationSecurityMetadataSource> getObjectType() {
        return FilterInvocationSecurityMetadataSource.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    protected LinkedHashMap<RequestMatcher, Collection<ConfigAttribute>> getResources() {
        LinkedHashMap<RequestMatcher, Collection<ConfigAttribute>> resourceMap = new LinkedHashMap<RequestMatcher, Collection<ConfigAttribute>>();

/*        List<Authority> resourceList = authorityService.getAll();
        if (resourceList != null && !resourceList.isEmpty()) {
            for (Authority authority : resourceList) {
                if (!Strings.isNullOrEmpty(authority.getPath())) {
                    String url = authority.getPath();

                    RequestMatcher matcher = new AntPathRequestMatcher(url);
                    Collection<ConfigAttribute> attributes = resourceMap.getOrDefault(matcher, new HashSet<>());

                    resourceMap.put(matcher, attributes);

                    attributes.add(new SecurityConfig(
                            String.format("%s_%s", ConstantValue.SECURITY_AUTH_PREFIX, authority.getId())
                    ));
                }
            }
        }*/

        return resourceMap;

    }

}
